package com.mobile.web.unemploymentDeclaration;

import com.mobile.service.dto.UnemploymentDeclarationDTO;
import com.mobile.service.dto.UnemploymentDeclarationQueryDTO;
import com.mobile.service.inter.UnemploymentDeclarationService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 失业保险申请表API接口
 */
@RestController
@RequestMapping("/unemploymentDeclaration")
@Tag(name = "失业保险申请管理接口", description = "包含失业保险申请的查询、新增、修改、删除等操作")
public class UnemploymentDeclarationController extends BaseController {

    @Autowired
    private UnemploymentDeclarationService unemploymentDeclarationService;

    @PostMapping("/addUnemploymentDeclaration")
    //@RequiresPermissions("unemploymentDeclarationmana:add:1")
    @Operation(summary = "创建失业保险申请")
    @AutoFill(OperateType.INSERT)
    public Object addUnemploymentDeclaration(@Valid @RequestBody UnemploymentDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = unemploymentDeclarationService.addUnemploymentDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getUnemploymentDeclarationListPage")
    //@RequiresPermissions("unemploymentDeclarationmana:read:1")
    @Operation(summary = "分页查询失业保险申请")
    public Object getUnemploymentDeclarationListPage(@Validated UnemploymentDeclarationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<UnemploymentDeclarationDTO> page = unemploymentDeclarationService.getUnemploymentDeclarationListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getUnemploymentDeclarationById")
    //@RequiresPermissions("unemploymentDeclarationmana:edit:1")
    @Operation(summary = "查询失业保险申请详情")
    public Object getUnemploymentDeclarationById(
        @Parameter(description = "失业保险申请主键", required = true) 
        @RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            UnemploymentDeclarationDTO dto = unemploymentDeclarationService.getUnemploymentDeclarationById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateUnemploymentDeclaration")
    //@RequiresPermissions("UnemploymentDeclarationmana:edit:1")
    @Operation(summary = "编辑失业保险申请")
    @AutoFill(OperateType.UPDATE)
    public Object updateUnemploymentDeclaration(@Valid @RequestBody UnemploymentDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = unemploymentDeclarationService.updateUnemploymentDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeUnemploymentDeclaration")
    //@RequiresPermissions("unemploymentDeclarationmana:delete:1")
    public Object removeUnemploymentDeclaration(@RequestParam("id") String id) {
        Map<String, Object> map;
        try {
            map = unemploymentDeclarationService.removeUnemploymentDeclaration(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getUnemploymentDeclarationList")
    //@RequiresPermissions("unemploymentDeclarationmana:read:1")
    @Operation(summary = "条件查询失业保险申请列表")
    public Object getUnemploymentDeclarationList(@Validated UnemploymentDeclarationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<UnemploymentDeclarationDTO> list = unemploymentDeclarationService.getUnemploymentDeclarationList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
